package com.smg.community.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.util.HtmlUtils;

import javacommon.util.FileUtil;

import com.smg.community.model.SmgTopic;
import com.smg.community.model.TopicTitleType;
import com.smg.community.setting.Cache;
import com.smg.community.setting.ProjectSetting;

public class ProjectUtil {
	
	
	public static String imageUploadPath = "";
	
	public static String imagePrefix = "";
	
	private static Properties p ;
	
	static 
	{
		 p = FileUtil.loadProperties("config.properties");
		 imageUploadPath = p.getProperty("imageUploadPath");
		 imagePrefix = p.getProperty("imagePrefix");
		ProjectSetting.USE_CACHE = p.getProperty("USE_CACHE").equals("true")?true:false;
	}
	
	/**
	 * 获取登录的用户名的类型
	 * @param loginName  
	 * 账号类型:
		1:昵称（原为卡账号）
		2:手机号
		3:邮箱
		4:卡号
	 * @return
	 */
	public static int getLoginNameType(String loginName){
	if ( StringUtil.isEmpty(loginName))
		return 0;
	if ( isMobileNO(loginName)){
		return Constant.LOGIN_TAGRT_MOBILE;
	}else if  ( isEmail(loginName)){
		return Constant.LOGIN_TAGRT_EMAIL;
	}
	return 1;
}	
		
	/**
	 *  获取用户注册输入的登录名类型  1 - 手机  2 - 邮箱
	 * @param userName
	 * @return
	 */
	public static int getRegTargetType(String loginName){
		if ( StringUtil.isEmpty(loginName))
			return 0;
		if ( isMobileNO(loginName)){
			return Constant.REG_TAGRT_MOBILE;
		}else if  ( isEmail(loginName)){
			return Constant.REG_TAGRT_EMAIL;
		}
		return 0;
	}
	 public static boolean isMobileNO(String mobiles){     
	        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");     
	        Matcher m = p.matcher(mobiles);     
	        return m.matches();     
	  } 
	 public static boolean isEmail(String email){     
	     String str="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
	        Pattern p = Pattern.compile(str);     
	        Matcher m = p.matcher(email);     
	        return m.matches();     
	    } 
	 
	 public static String getTopicTitleName(int topicTitleId){
		List<TopicTitleType> list =  Cache.TopicTitleTypeList;
		for ( TopicTitleType t :list){
			if ( t.getTitleTypeId() == topicTitleId)
				return t.getTitleTypeName();
		}
		return "据说";
	 }
	 public static void filterTopic(SmgTopic topic){
		if ( topic.getTopicTitleAfterFilter() != null){
			 topic.setTopicTitleAfterFilter(  HtmlUtils.htmlEscape(topic.getTopicTitleAfterFilter()) );
		}
		if ( topic.getCreateMemberName() != null){
			topic.setCreateMemberName( HtmlUtils.htmlEscape(topic.getCreateMemberName())  );
		}
		if  ( topic.getLastReplyMemberName() != null ){
			topic.setLastReplyMemberName(  HtmlUtils.htmlEscape(topic.getLastReplyMemberName())  );
		}
	 }

	public static String getTopicDetailUrl(HttpServletRequest request,int topicId) {
		String url = request.getContextPath() +"/topic/detail/"+topicId;
		return url;
	}

	/**
	 * 将内容里的用户上传的图片通过正则过滤出来 如果没有的话则设为默认图片
	 * @param topicContAfterFilter
	 * @return
	 * /**
	 * 
	 * @param context 需要提取图片路径的字符串
	 * @return 字符串数组(imagePath[]其length为3)的集合
	 * <img src="http://localhost:8080/images/small/2013-05-02/222419553.jpg" alt="" border="0" />
	 * 格式类似：  imagePath[0] = "/userimages/normal/" 该字符串内容固定，对应表group_image中的imgbasepath字段
	 * 			 imagePath[1] = "2011-06-10"对应表group_image中的imgdatapath字段
	 * 			 imagePath[2] = "2011-06-10-1307671080500.jpg"对应表group_image中imgname字段
	 * 
	 */
	public static String filterImages(String context) {
		String imageUrl = null;
		String reg = "images/small/[0-9]{4}-[0-9]{2}\\-[0-9]{2}/[0-9]{9}.[jpg|bmp|gif|png|jpeg|JPG|BMP|GIF|PNG|JPEG]+";
		Matcher matcher = Pattern.compile(reg).matcher(context);
		while (matcher.find()) {
			imageUrl = matcher.group();
			// String imgTag = context.substring(context.indexOf(imageUrl) - src="http://121.199.4.98:8888/images/650/2013-05-21/163424239.png"
			// 10,context.indexOf(imageUrl));
			//System.out.println(imgTag);
			return imageUrl;
		}
		if ( imageUrl == null){
			 reg = "images/650/[0-9]{4}-[0-9]{2}\\-[0-9]{2}/[0-9]{9}.[jpg|bmp|gif|png|jpeg|JPG|BMP|GIF|PNG|JPEG]+";
			 matcher = Pattern.compile(reg).matcher(context);
			 while (matcher.find()) {
					imageUrl = matcher.group();
					// String imgTag = context.substring(context.indexOf(imageUrl) -
					// 10,context.indexOf(imageUrl));
					//System.out.println(imgTag);
					return imageUrl;
				}
		}
		
		return imageUrl;
	}
	
	public static void main(String[] args){
		System.out.println(date2Constellation(11,1));
		System.out.println(date2Constellation(11,26));	
		String imageUrl =  getImageFromTopicCont(" src=http://121.199.4.98:8888/images/650/2013-05-21/163424239.png");
		System.out.println(imageUrl);
	}
	public static String getImageFromTopicCont(String topicContAfterFilter) {
		String  url  =  filterImages(topicContAfterFilter);
		if  ( url == null){
		}else{
			url = ProjectUtil.imagePrefix + url ;
		}
		return  url;
	}
	 public static final String[] constellationArr = { "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座",   
         "天蝎座", "射手座", "魔羯座" };   
	 public static final String[] zodiacArr = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊" };   
	 
 public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };   
   
 /**  
  * 根据日期获取生肖  
  * @return  
  */  
 public static String date2Zodica(Calendar time) {   
     return zodiacArr[time.get(Calendar.YEAR) % 12];   
 }   
   
 /**  
  * 根据日期获取星座  
  * @param time  
  * @return  
  */  
 public static String date2Constellation(int month,int day) {   
	 if  ( month == 0 ){
		 month = 1 ;
	 }
	 month = month - 1;
     if (day < constellationEdgeDay[month]) {   
         month = month - 1;   
     }   
     if (month >= 0) {   
         return constellationArr[month];   
     }   
     //default to return 魔羯   
     return constellationArr[11];   
 }
 
}
